/**
 * Created by KingKong on 2017/8/9.
 */
import {AbstractControl, ValidatorFn} from '@angular/forms';

/**
 * 自定义验证规则：日期格式，日期格式无效时，验证失败，如果允许为空，空值不验证日期格式
 * */
export function customizeDateTimeValidator(validators: { [key: string]: string }, key: string): ValidatorFn {
  return (control: AbstractControl): { [key: string]: any } => {
    if (!validators.hasOwnProperty('required') && control.value === null || typeof control.value === 'undefined' || control.value === '') {
      return null;
    } else {
      const ret = new Date(control.value);
      return ret.toString() !== 'Invalid Date' ? null : {[key]: true};
    }
  };
}
